package com.sinosoft.perfect.system._unit.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;

import com.sinosoft.perfect.common.system.BaseAction;
import com.sinosoft.perfect.system._.po.TUnit;
import com.sinosoft.perfect.system._.po.TUnitInformation;
import com.sinosoft.perfect.system._.po.TUserRegisterNum;
import com.sinosoft.perfect.system._unit.dataFormat.IUnitGridDataFormat;
import com.sinosoft.perfect.system._unit.dataFormat.IUnitTreeDataFormat;
import com.sinosoft.perfect.system._unit.formBean.UnitForm;
import com.sinosoft.perfect.system._unit.service.IUnitService;

@SuppressWarnings("serial")
public class UnitAction extends BaseAction {

	private IUnitService unitService;

	private IUnitGridDataFormat unitGridDataFormat;

	private IUnitTreeDataFormat unitTreeDataFormat;

	private TUnit tunit;

	private TUnitInformation tunitinfo;

	private TUserRegisterNum tuserregisternum;

	private String unitId;

	private String[] unitIds;

	/**
	 * 构建单位树
	 */
	public String unitTree() throws IOException {
		response.setContentType("text/xml;charset=UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		String resultXml = null;
		try {
			List<UnitForm> units = unitService.selectTUnit(tunit);
			resultXml = unitTreeDataFormat.dataFormatUnitTree(units);

		} catch (SQLException e) {
			e.printStackTrace();
			session.put("exceptionInfoClass", e);
			return "exceptionOperateAction";
		}
		PrintWriter out = response.getWriter();
		out.print(resultXml);
		out.flush();
		out.close();
		return null;
	}

	public String unitGrid() throws IOException {
		response.setContentType("text/xml;charset=UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		String resultXml = null;
		try {
			List<UnitForm> units = unitService.selectTUnit(tunit);
			resultXml = unitGridDataFormat.dataFormatUnitGrid(units);

		} catch (SQLException e) {
			e.printStackTrace();
			session.put("exceptionInfoClass", e);
			return "exceptionOperateAction";
		}
		PrintWriter out = response.getWriter();
		out.print(resultXml);
		out.flush();
		out.close();
		return null;
	}

	public String unitQueryForEdit() {
		try {
			TUnit unit = new TUnit();
			unit.setUnitId(unitId);
			List<UnitForm> units = unitService.selectTUnit(unit);

			if (units != null && units.size() != 0) {
				request.setAttribute("unitEdit", units.get(0));
			}

		} catch (SQLException e) {
			e.printStackTrace();
			session.put("exceptionInfoClass", e);
			return "exceptionOperateAction";
		}

		return "unitEdit";
	}

	public String unitEdit() {
		try {
			unitService.updateTUnitByPK(tunit);
			this.unitId = tunit.getUnitId();// 更新后，通过unitId查询

		} catch (SQLException e) {
			e.printStackTrace();
			session.put("exceptionInfoClass", e);
			return "exceptionOperateAction";
		}

		return unitQueryForEdit();
	}

	public String unitDelFromTree() {
		try {
			unitService.deleteTUnits(new String[] { unitId });

		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

	public String unitInfoQueryForEdit() {
		try {
			TUnitInformation unitInfo = new TUnitInformation();
			unitInfo.setUnitId(unitId);

			List<TUnitInformation> unitInfos = unitService.selectTUnitInfo(unitInfo);
			request.setAttribute("unitInfoEdit", unitInfos.get(0));

		} catch (SQLException e) {
			e.printStackTrace();
			session.put("exceptionInfoClass", e);
			return "exceptionOperateAction";
		}

		return "unitInfoEdit";
	}

	public String unitInfoEdit() {
		try {
			unitService.updateTUnitInfoByPK(tunitinfo);
			this.unitId = tunitinfo.getUnitId();// 更新后，通过unitId查询

		} catch (SQLException e) {
			e.printStackTrace();
			session.put("exceptionInfoClass", e);
			return "exceptionOperateAction";
		}

		return unitInfoQueryForEdit();
	}

	public String unitInfoEditClear() {
		try {
			TUnitInformation unitInfo = new TUnitInformation();
			unitInfo.setId(tunitinfo.getId());
			unitInfo.setUnitId(tunitinfo.getUnitId());
			unitService.updateTUnitInfoByPK(unitInfo);
			this.unitId = tunitinfo.getUnitId();// 更新后，通过unitId查询

		} catch (SQLException e) {
			e.printStackTrace();
			session.put("exceptionInfoClass", e);
			return "exceptionOperateAction";
		}

		return unitInfoQueryForEdit();
	}

	public String userRegisterNumQueryForEdit() {
		try {
			TUserRegisterNum userRegisterNum = new TUserRegisterNum();
			userRegisterNum.setUnitId(unitId);
			List<TUserRegisterNum> userRegisterNums = unitService.selectTUserRegisterNum(userRegisterNum);
			if (userRegisterNums != null && userRegisterNums.size() != 0) {
				request.setAttribute("userRegisterNumEdit", userRegisterNums.get(0));
			} else {
				request.setAttribute("unitId", unitId);
				return "userRegisterNumAdd";
			}

		} catch (SQLException e) {
			e.printStackTrace();
			session.put("exceptionInfoClass", e);
			return "exceptionOperateAction";
		}

		return "userRegisterNumEdit";
	}

	public String userRegisterNumEdit() {
		try {
			unitService.updateTUserRegisterNumByPK(tuserregisternum);
			this.unitId = tuserregisternum.getUnitId();// 更新后，通过unitId查询

		} catch (SQLException e) {
			e.printStackTrace();
			session.put("exceptionInfoClass", e);
			return "exceptionOperateAction";
		}

		return userRegisterNumQueryForEdit();
	}

	public String userRegisterNumNew() {
		try {
			unitService.insertTUserRegisterNum(tuserregisternum);
			this.unitId = tuserregisternum.getUnitId();// 增加后，通过unitId查询

		} catch (SQLException e) {
			e.printStackTrace();
			session.put("exceptionInfoClass", e);
			return "exceptionOperateAction";
		}

		return userRegisterNumQueryForEdit();
	}

	public String userRegisterNumDel() {
		try {
			unitService.deleteTUserRegisterNumByUnitId(unitId);

		} catch (SQLException e) {
			e.printStackTrace();
			session.put("exceptionInfoClass", e);
			return "exceptionOperateAction";
		}

		return userRegisterNumQueryForEdit();
	}

	/**
	 * 
	 */

	public void setUnitService(IUnitService unitService) {
		this.unitService = unitService;
	}

	public void setUnitGridDataFormat(IUnitGridDataFormat unitGridDataFormat) {
		this.unitGridDataFormat = unitGridDataFormat;
	}

	public void setUnitTreeDataFormat(IUnitTreeDataFormat unitTreeDataFormat) {
		this.unitTreeDataFormat = unitTreeDataFormat;
	}

	/**
	 * 
	 */

	public String getUnitId() {
		return unitId;
	}

	public void setUnitId(String unitId) {
		this.unitId = unitId;
	}

	public TUnit getTunit() {
		return tunit;
	}

	public void setTunit(TUnit tunit) {
		this.tunit = tunit;
	}

	public String[] getUnitIds() {
		return unitIds;
	}

	public void setUnitIds(String[] unitIds) {
		this.unitIds = unitIds;
	}

	public TUnitInformation getTunitinfo() {
		return tunitinfo;
	}

	public void setTunitinfo(TUnitInformation tunitinfo) {
		this.tunitinfo = tunitinfo;
	}

	public TUserRegisterNum getTuserregisternum() {
		return tuserregisternum;
	}

	public void setTuserregisternum(TUserRegisterNum userRegisterNum) {
		tuserregisternum = userRegisterNum;
	}

}
